<template>
  <div class="containerBox">
    <div>
      <span>{{ drag ? "拖拽中" : "拖拽停止" }}</span>
      <el-button
        @click="handleOpen"
        type="primary"
        size="mini"
        icon="el-icon-s-tools"
        round
        style="float: right;"
        >设置</el-button
      >
    </div>
    <br>
    <el-row style="min-height: 600px;">
      <el-col :span="8">
        <draggable
          v-model="myArray1"
          ghost-class="dragClass"
          chosen-class="chosenClass"
          drag-class="dragClass"
          :group="groupA"
          animation="300"
          @start="onStart"
          @end="onEnd"
          :forceFallback="true"
          :fallbackTolerance="5"
        >
          <transition-group class="chartBox">
            <div class="item" v-for="element in myArray1" :key="element.id">
              <span>{{ element.name }}</span>
              <el-button
                size="mini"
                icon="el-icon-circle-close"
                circle
                style="position: absolute; right: 0; top: 0"
                @click="delItem(element, myArray1)"
              ></el-button>
            </div>
          </transition-group>
        </draggable>
      </el-col>
      <el-col :span="8">
        <draggable
          v-model="myArray2"
          ghost-class="dragClass"
          chosen-class="chosenClass"
          drag-class="dragClass"
          :group="groupB"
          animation="300"
          @start="onStart"
          @end="onEnd"
          :forceFallback="true"
          :fallbackTolerance="5"
        >
          <transition-group class="chartBox">
            <div class="item" v-for="element in myArray2" :key="element.id">
              <span>{{ element.name }}</span>
              <el-button
                size="mini"
                icon="el-icon-circle-close"
                circle
                style="position: absolute; right: 0; top: 0"
                @click="delItem(element, myArray2)"
              ></el-button>
            </div>
          </transition-group>
        </draggable>
      </el-col>
      <el-col :span="8">
        <draggable
          v-model="myArray3"
          ghost-class="dragClass"
          chosen-class="chosenClass"
          drag-class="dragClass"
          :group="groupC"
          animation="300"
          @start="onStart"
          @end="onEnd"
          :forceFallback="true"
          :fallbackTolerance="5"
        >
          <transition-group class="chartBox">
            <div class="item" v-for="element in myArray3" :key="element.id">
              <span>{{ element.name }}</span>
              <el-button
                size="mini"
                icon="el-icon-circle-close"
                circle
                style="position: absolute; right: 0; top: 0"
                @click="delItem(element, myArray3)"
              ></el-button>
            </div>
          </transition-group>
        </draggable>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="item" v-for="(element) in myArray5" :key="element.id">
          <span>{{ element.name }}</span>
        </div>
      </el-col>
    </el-row>
    <el-drawer
      class="lzcElDrawer"
      title="缩略图"
      size="15%"
      :visible.sync="drawerFlag"
      :direction="direction"
      :before-close="handleClose"
      :modal="false"
      :wrapperClosable="false"
      :close-on-press-escape="false"
    >
      <draggable
        v-model="myArray4"
        ghost-class="dragClass"
        chosen-class="chosenClass"
        drag-class="dragClass"
        :group="groupD"
        animation="300"
        @start="onStart"
        @end="onEnd"
        :forceFallback="true"
        :fallbackTolerance="5"
      >
        <transition-group class="chartBox chartBoxOther">
          <div class="itemOther" v-for="(element, idx) in myArray4" :key="element.id">
            <img :src="diagram[idx + 1]">
            <div>{{ element.name }}</div>
          </div>
        </transition-group>
      </draggable>
    </el-drawer>
  </div>
</template>

<script>
/**
 * draggable 组件的 forceFallback 和 fallbackTolerance 属性说明，这2者一般配合使用
 * forceFallback
 *    核心作用是强制组件使用自定义的拖拽降级逻辑，而非依赖浏览器原生的 dragable api 实现拖拽功能
 *        1、统一跨浏览器体验
 *        2、自定义拖拽交互
 *        3、兼容特殊场景
 * fallbackTolerance
 *    核心作用是设定一个"触发阈值"，只有拖拽位移操作超过这个值时，才会强制启用 forceFallback 自定义降级逻辑
 *        1、过滤微小位移的影响
 *        2、精准触发拖拽
 *        3、适配不同场景
 * 比如：
 *        draggable组件不加这2个属性时，拖拽起来的块，有个向右的渐变透明度
 */
import draggable from 'vuedraggable';
export default {
  name: 'DragableComp',
  data() {
    return {
      drag: false,
      // 随意简图
      diagram: {
        1: require('@/assets/imgs/diagram/rose.png'),
        2: require('@/assets/imgs/diagram/pie1.png'),
        3: require('@/assets/imgs/diagram/pie2.png'),
        4: require('@/assets/imgs/diagram/lineOne.png'),
        5: require('@/assets/imgs/diagram/lineTwo.png'),
        6: require('@/assets/imgs/diagram/data1.png'),
        7: require('@/assets/imgs/diagram/table.png'),
        8: require('@/assets/imgs/diagram/top.png'),
        9: require('@/assets/imgs/diagram/trend.png')
      },
      myArray1: [
        { id: 1, name: 'www.1.com' },
        { id: 2, name: 'www.2.com' },
        { id: 3, name: 'www.3.com' }
      ],
      myArray2: [
        { id: 4, name: 'www.4.com' },
        { id: 5, name: 'www.5.com' },
        { id: 6, name: 'www.6.com' }
      ],
      myArray3: [
        { id: 7, name: 'www.7.com' },
        { id: 8, name: 'www.8.com' },
        { id: 9, name: 'www.9.com' }
      ],
      myArray4: [],
      // myArray5主要做占位测试用
      myArray5: [
        { id: 10, name: 'www.10.com' }
      ],
      direction: 'rtl',
      drawerFlag: false, // 是否处于拖拽中
      // name相同的组之间，可以相互拖动，pull表示是否允许拖出去，put表示是否允许拖进来
      groupA: {
        name: 'itxst',
        pull: true,
        put: (...args) => {
          let dragItem = args[2];
          if (this.myArray1.map(ls => ls.name).includes(dragItem.innerText)) {
            return true; // 自己这个group的组员之间，在满员时，能够相互调换位置
          } else if (this.myArray1.length === 3) {
            return false;
          }
        }
      },
      groupB: {
        name: 'itxst',
        pull: true,
        put: (...args) => {
          let dragItem = args[2];
          if (this.myArray2.map(ls => ls.name).includes(dragItem.innerText)) {
            return true; // 自己这个group的组员之间，在满员时，能够相互调换位置
          } else if (this.myArray2.length === 3) {
            return false;
          }
        }
      },
      groupC: {
        name: 'itxst',
        pull: true,
        put: (...args) => {
          let dragItem = args[2];
          if (this.myArray3.map(ls => ls.name).includes(dragItem.innerText)) {
            return true; // 自己这个group的组员之间，在满员时，能够相互调换位置
          } else if (this.myArray3.length === 3) {
            return false;
          }
        }
      },
      groupD: {
        name: 'itxst',
        pull: true,
        put: true
      }
    };
  },
  components: {
    draggable
  },
  methods: {
    onStart() {
      this.drag = true;
    },
    onEnd() {
      this.drag = false;
    },
    delItem(item, ary) {
      ary.forEach((ls, index, arr) => {
        if (item.id === ls.id) {
          arr.splice(index, 1);
        }
      });
      this.myArray4.push(item);
    },
    handleOpen() {
      this.drawerFlag = true;
    },
    handleClose() {
      this.drawerFlag = false;
    }
  }
};
</script>

<style lang="less" scoped>
.containerBox {
  .item {
    position: relative;
    width: 100%;
    height: 200px;
    background-color: #666;
    color: #000;
    line-height: 200px;
    border: 1px solid #000;
    user-select: none;
    cursor: move;
  }
  .chartBox {
    width: 100%;
    min-height: 50px;
    display: block;
    box-sizing: border-box;
    border: 1px solid lime;
    &.chartBoxOther {
      min-height: 600px;
    }
    .item {
      position: relative;
      width: 100%;
      height: 200px;
      background-color: #666;
      color: #000;
      line-height: 200px;
      border: 1px solid #000;
      user-select: none;
      cursor: move;
    }
    .itemOther {
      width: 100%;
      text-align: center;
      margin: 0 auto;
      box-sizing: border-box;
      img {
        width: 100%;
        height: 149px;
      }
    }
    // 以下样式，为了防止不生效，需要加上 !important
    // 拖拽时，原位置元素的样式
    .ghostClass {
      background-color: blue !important;
    }
    // 点击时，选中的元素样式，和拖拽到其他元素上方时的样式
    .chosenClass {
      background-color: #fff !important;
      opacity: 1 !important;
    }
    // 拖拽元素时的样式
    .dragClass {
      background-color: blueviolet !important;
      opacity: 1 !important;
      box-shadow: none !important;
      outline: none !important;
      background-image: none !important;
    }
  }
}
.lzcElDrawer {
  pointer-events: none;
  /deep/.el-drawer.rtl {
    pointer-events: all;
  }
}
</style>